package com.leetcode.No0801;

import org.junit.Test;

/**
 * @program: Solution
 * @description: 使序列递增的最小交换次数
 * @author: Wang Zhihua
 * @date: 2022-10-10
 */
public class Solution2 {
    public int minSwap(int[] nums1, int[] nums2) {
        int len = nums1.length;
        int ans = 0;

        int curNum1 = nums1[0];
        int curNum2 = nums2[0];
        for (int i = 1; i < len; ++i) {
            if (curNum1 < nums1[i] && curNum2 < nums2[i]) {
                curNum1 = nums1[i];
                curNum2 = nums2[i];
            } else {
                ++ans;
                if (i == 1 || (nums2[i - 2] < curNum1 && nums1[i - 2] < curNum2)) {
                    curNum1 = nums1[i];
                    curNum2 = nums2[i];
                } else {
                    curNum2 = nums1[i];
                    curNum1 = nums2[i];
                }
            }
        }

        return Math.min(ans, len - ans);
    }

    @Test
    public void test1() {
        int[] nums1 = new int[]{3,3,8,9,10};
        int[] nums2 = new int[]{1,7,4,6,8};
        System.out.println(minSwap(nums1, nums2));
    }
}
